package com.cj.ctyl.framework.core;

import java.util.HashMap;

/**
 * @Author: Alex
 * @Description: 统一返回
 * @Date: 2021/8/15
 */
public class Result extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;
    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";

    /**
     * 状态类型
     */
    public enum Type {
        /**
         * 成功
         */
        SUCCESS(200),
        /**
         * 警告
         */
        WARN(301),
        /**
         * 错误
         */
        ERROR(500);
        private final int value;

        Type(int value) {
            this.value = value;
        }

        public int value() {
            return this.value;
        }
    }


    public Result() {

    }

    public Result(Type type, String msg) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
    }

    public Result(Type type, String msg, Object data) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }


    public static Result success() {
        return Result.success("请求成功!");
    }

    public static Result success(String msg) {
        return Result.success(msg, null);
    }

    public static Result success(Object data) {
        return new Result(Type.SUCCESS, "请求成功!", data);
    }

    public static Result success(String msg, Object data) {
        return new Result(Type.SUCCESS, msg, data);
    }


    public static Result warn(String msg) {
        return Result.warn(msg, null);
    }

    public static Result warn(String msg, Object data) {
        return new Result(Type.WARN, msg, data);
    }


    public static Result error() {
        return Result.error("请求失败!");
    }

    public static Result error(String msg) {
        return Result.error(msg, null);
    }

    public static Result error(String msg, Object data) {
        return new Result(Type.ERROR, msg, data);
    }


}
